<template>
	<view class="address u-skeleton">
		<u-navbar title="收货地址" :background="{
			'backgroundColor':'#fff'
		}" :borderBottom="false" class="u-skeleton-rect"></u-navbar>
			<view class="item u-skeleton-rect" v-for="(item,index) in list" :key="index">
				<view class="name"> 收货人：{{item.name + item.mobile}}</view>
				<view class="i-address"> 收货地址：{{item.province + item.city + item.district + item.detail}}</view>
				<view class="bottom">
					<view class="left">
						<u-checkbox @change="checkboxChange(item,index)" v-model="item.is_default == 2 ? true : false " width="120" shape="circle" active-color="#000"></u-checkbox>
						<text>设为默认地址</text>
					</view>
					<view class="right">
						<view class="btn-edit" @click="edit(item)">编辑</view>
						<view class="btn-edit" @click="del(item,index)">删除</view>
					</view>
				</view>
			</view>
		<u-button class="btn-primary u-skeleton-rect" @click="createAddress" ripple-bg-color="rgba(0,0,0,.4)" :ripple="true">添加收货地址</u-button>
		<u-skeleton :loading="loding" :animation="true" bgColor="#FFF" ></u-skeleton>
	</view>
</template>
<script>
	export default {
		data(){
			return {
				loding:true,//初始化骨架屏展示
				checked:false, //判断选中了那一个地址
				list:[] //地址列表
			}
		},
		onShow() {
			this.getinfo();
		},
		methods:{
			createAddress(result) {
				this.$util.redirectTo('/pages/my/addAddress');
			},
			//用户地址默认修改
			checkboxChange(item,index){
				this.$http.asyncSendrequest({
					url:'/profile/saveDefault',
					data:{
						id:item.id,
						is_default:2
					},
					success:res=>{
						this.$util.taost({
							title:res.msg,
						})
						if(res.code == 0){
							this.getinfo();
						}
					}
				})
			},
			//编辑
			edit(item){
				this.$util.redirectTo('/pages/my/addAddress',{str:JSON.stringify(item)});
			},
			//删除
			del(item,index){
				uni.showModal({
					title:'',
					content:'是否确定删除',
					showCancel:true,
					success:v=>{
						if(v.confirm){
							this.$http.asyncSendrequest({
								url:'/profile/delAddress',
								data:{
									id:item.id
								},
								success:res=>{
									this.$util.taost({
										title:res.msg,
										callback:n=>{
											if(res.code == 0) {
												this.list.splice(index,1)
											}
										}
									})
								}
							})
						}
					}
				})
			},
			//获取用户地址
			getinfo(){
				this.$http.asyncSendrequest({
					url:'/profile/addressList',
					data:{
						page:1,
						page_size:20
					},
					success:res=>{
						if(res.code == '0') {
							this.list = res.data;
						}
						this.loding = false;
					}
				})
			}
			
		}
	}
</script>

<style lang="scss" scoped>
	.item{
		margin: 20rpx 0;
		background: #FFFFFF;
		padding: 30rpx;
		box-sizing: border-box;
		.name{
			font-size: 26rpx;
		}
		.i-address{
			margin: 20rpx 0;
		}
		.bottom{
			display: flex;
			align-items: center;
			justify-content: space-between;
			.left{
				display: flex;
				align-items: center;
				font-size: 26rpx;
			}
			.right{
				display: flex;
				align-items: center;
				.btn-edit{
					width: auto;
					padding: 4rpx 10rpx;
					border-radius: 4rpx;
					font-size: 24rpx;
					border:1rpx solid #292929;
					margin-left: 20rpx;
				}
			}
		}
	}
	.btn-primary {
		height: 96rpx;
		background: #292929;
		border-radius: 10rpx;
		text-align: center;
		line-height: 96rpx;
		font-size: 26rpx;
		font-family: PingFang SC;
		color: #FFFFFF;
		margin-top: 116rpx;
		position: fixed;
		bottom: 20rpx;
		left: 30rpx;
		width: calc(100% - 60rpx);
	}
</style>